using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Reflection;
using System.Resources;

using EnvDTE;

using ClassBuilderWizard.Interfaces;

namespace ClassBuilderWizard.DefaultPlugins
{
    public class MSSQLPlugin : IDatabasePlugin, IDisposable
    {
        private ProjectType[] _SupportedProjectTypes;

        public MSSQLPlugin()
        {
            this._SupportedProjectTypes = new ProjectType[] { ProjectType.CSharp, ProjectType.VB };
        }

        #region IPlugin Members

        public string Name
        {
            get { return "MS SQL Server"; }
        }

        public string Version
        {
            get { return "3.0"; }
        }

        public string Author
        {
            get { return "Jason Williams"; }
        }

        public ProjectType[] SupportedProjectTypes
        {
            get { return this._SupportedProjectTypes; }
        }

        public DatabaseObject ExecutePlugin(Parameters parameters)
        {
            //the object returned to the wizard
            DatabaseObject dbo = new DatabaseObject();

            //start the first form to collect the database connection string
            Forms.frmMSSQLWizardStep1 frm1 = new ClassBuilderWizard.DefaultPlugins.Forms.frmMSSQLWizardStep1();
            if (frm1.ShowDialog() == DialogResult.OK)
            {
                //the user wants to proceed
                string conn = frm1.ConnectionString;
                parameters.CreateDAL = frm1.GenerateDataComponents;
                parameters.UseDatabaseNulls = frm1.PreserveNulls;

                Forms.frmMSSQLWizardStep2 frm2 = new ClassBuilderWizard.DefaultPlugins.Forms.frmMSSQLWizardStep2(conn, parameters);
                DialogResult res = frm2.ShowDialog();
                if (res == DialogResult.OK)
                {
                    //next or finish was clicked
                    dbo = frm2.SelectedObject;
                    if (frm1.UseObjectNameAsClassName)
                        parameters.NewItemName = dbo.Name;

                    if (parameters.CreateDAL)
                    {
                        //create the data access layer class
                        DALClass dc = new DALClass(dbo, parameters);
                        dc.CreateContent();

                        SQLScript ss = new SQLScript(dbo, parameters);
                        ss.CreateContent();
                    }
                }
                else if (res == DialogResult.Retry)
                {
                    //back was clicked
                    return ExecutePlugin(parameters);
                }
                else
                {
                    //cancel was clicked
                }
            }

            return dbo;
        }

        public Bitmap Icon
        {
            get
            {
                return Images.DataSet_TableView;
            }
        }

        public PluginType PluginType
        {
            get
            {
                return PluginType.Database;
            }
        }

        #endregion

        public override string ToString()
        {
            return string.Format("{0} ({1})", this.Name, this.Version);
        }

        #region IDisposable Members

        public void Dispose()
        {
            this._SupportedProjectTypes = null;
        }

        #endregion
    }
}
